//
//  XS_KuaiXueDetailVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/28.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import AliyunPlayer

class XS_KuaiXueDetailVC: XS_BaseVC {
    var id: String!
    var player: AliPlayer?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        player?.start()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        player?.pause()
    }
    
    override func upConfig() {
        shouldNavBarHidden = true
        isStatusBarBlack = false
    }
    
    override func upView() {
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = view.bounds.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collection.showsVerticalScrollIndicator = false
        collection.showsHorizontalScrollIndicator = false
        collection.contentInsetAdjustmentBehavior = .never
        collection.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
        collection.bounces = false
        view.addSubview(collection)
        collection.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
        collection.isPagingEnabled = true
        collection.register(XS_TuijianCell.self, forCellWithReuseIdentifier: "cell")
        
        let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, XS_queryStarM>>(configureCell: { [weak self] ds, cv, ip, it in
            let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: ip) as! XS_TuijianCell
            cell.modelPS.onNext(it)
            cell.loadUrl(it.videoUrl, isAutoPlay: true)
            if self?.player == nil { self?.player = cell.player }
            return cell
        })
        XS_NetWork.requestJson("/quick/queryQuickLearnById", parameters: ["id":id ?? ""])
            .mapModel(type: XS_queryStarM.self)
            .asDriver(onErrorJustReturn: (nil, XS_Response()))
            .filter { $0.1.success && $0.0 != nil }
            .compactMap { $0.0 }
            .flatMapLatest { Driver.just([SectionModel<String, XS_queryStarM>(model: "", items: [$0])]) }
            .drive(collection.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
        
        loadCustomBar()
    }
}
